package com.quanyue.haircut.order.service.impl;

import com.quanyue.haircut.order.dto.ArriveShopCount;
import com.quanyue.haircut.order.dto.CommissionResult;
import com.quanyue.haircut.order.dto.TechnicianCommission;
import com.quanyue.haircut.order.dto.TurnoverResult;
import com.quanyue.haircut.order.mapper.ReserveInfoMapper;
import com.quanyue.haircut.order.mapper.ReserveOrderMapper;
import com.quanyue.haircut.order.service.OrderCountService;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderCountServiceImpl implements OrderCountService {
    private final String path = "/opt/haircut/server/static/excel/";
    @Autowired
    private ReserveInfoMapper reserveInfoMapper;
    @Autowired
    private ReserveOrderMapper reserveOrderMapper;

    @Override
    public CommissionResult findCommission(Integer technicianId) {
        List <TechnicianCommission> technicianCommissions = reserveInfoMapper.findCommission(technicianId);
        BigDecimal totalCommission = BigDecimal.ZERO;
        technicianCommissions.forEach(technicianCommission -> {
            totalCommission.add(technicianCommission.getPayCommission());
        });
        CommissionResult result = new CommissionResult();
        result.setTechnicianCommissions(technicianCommissions);
        result.setTotalCommission(totalCommission);
        return result;
    }

    @Override
    public List <TurnoverResult> dateTurnover(Integer merchantNo, LocalDate startDate, LocalDate endDate) {
        List <TurnoverResult> shopTurnovers = reserveOrderMapper.findDateTurnover(merchantNo, startDate, endDate);
        return shopTurnovers;
    }

    @Override
    public List <TurnoverResult> monthTurnover(Integer merchantNo, String startMonth, String endMonth) {
        List <TurnoverResult> result = reserveOrderMapper.findMonthTurnover(merchantNo, startMonth, endMonth);
        return result;
    }

    @Override
    public BigDecimal merchantTurnover(Integer merchantNo) {
        return reserveOrderMapper.findTurnoverByMerchant(merchantNo);
    }

    @Override
    public List <ArriveShopCount> dateArriveShop(Integer merchantNo, LocalDate startDate, LocalDate endDate) {
        return reserveOrderMapper.findDateArriveShop(merchantNo, startDate, endDate);
    }

    @Override
    public List <ArriveShopCount> monthArriveShop(Integer merchantNo, String startMonth, String endMonth) {
        return reserveOrderMapper.findMonthArriveShop(merchantNo, startMonth, endMonth);
    }

    @Override
    public Map <String, BigDecimal> findTotalMonthDate(Integer merchantNo) {
        Map <String, BigDecimal> result = new HashMap <>();
        BigDecimal total = reserveOrderMapper.findTurnoverByMerchant(merchantNo);
        BigDecimal month = reserveOrderMapper.findTurnoverNow(merchantNo, String.valueOf(LocalDate.now()).substring(0, 7));
        BigDecimal date = reserveOrderMapper.findTurnoverNow(merchantNo, String.valueOf(LocalDate.now()));
        result.put("total", total == null ? BigDecimal.ZERO : total);
        result.put("month", month == null ? BigDecimal.ZERO : month);
        result.put("date", date == null ? BigDecimal.ZERO : date);
        return result;
    }

    @Override
    public String createExcel(Integer merchantNo) {
        Map <String, BigDecimal> map = this.findTotalMonthDate(merchantNo);
        try {
            String fileName = "Turnover.xls";
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            //  打开文件
            WritableWorkbook book = Workbook.createWorkbook(new File(path, fileName));
            //  生成名为“第一页”的工作表，参数0表示这是第一页
            WritableSheet sheet = book.createSheet(" 第一页 ", 0);

            //  将定义好的单元格添加到工作表中  在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
            sheet.addCell(new Label(0, 0, "总营业额"));
            sheet.addCell(new Label(1, 0, "月营业额"));
            sheet.addCell(new Label(2, 0, "日营业额"));
            int row = 1;
            for (int i = 0; i < map.size(); i++) {
                int column = 0;
                sheet.addCell(new Label(column++, row, String.valueOf(map.get("total"))));
                sheet.addCell(new Label(column++, row, String.valueOf(map.get("month"))));
                sheet.addCell(new Label(column++, row, String.valueOf(map.get("date"))));
                ++row;
            }
            //  写入数据并关闭文件
            book.write();
            book.close();
            return path + fileName;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
